// withdrawCash.js
var app = getApp()
Page({

    /**
     * 页面的初始数据
     */
    data: {

    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function(options) {

        var that = this;
        wx.request({
            url: app.data.baseUrl + '/AibraUsb/withdraw/addUI.do',
            data: {
                openId: app.globalData.openid
            },
            success: function(res) {
                console.log(res);
                that.setData({
                    balance: res.data.balance,
                    bankCardName: res.data.bankCardName,
                    bankCardNo: res.data.bankCardNo,
                    bankCardId: res.data.id

                })
            }
        })
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function() {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function() {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function() {

    },
    chooseCard: function() {
        wx.redirectTo({
            url: "chooseBankcard/chooseBankcard"
        })
    },
    cashNum: function(e) {
        var that = this;
        console.log(e.detail.value)
        that.setData({
            cashNum: e.detail.value
        })
    },
    withdrawcash: function() {
        var that = this;
        if (that.data.cashNum)
            wx.request({
                url: app.data.baseUrl + '/AibraUsb/withdraw/add.do',
                data: {
                    openId: app.globalData.openid,
                    bankCardId: that.data.bankCardId,
                    money: that.data.cashNum
                },
                success: function(res) {
                    if (res.msg == 0) {
                        wx.showToast({
                            title: '银行卡编号有误或者用户不存在'
                        })
                    } else if (res.msg == 2) {
                        wx.showToast({
                            title: '余额不足'
                        })
                    } else {
                        if (that.data.balance <= that.data.cashNum) {
                            wx.showToast({
                                title: '余额不足'
                            })
                            return
                        }

                        wx.showToast({
                            title: '正在提现中'
                        })
                        wx.redirectTo({
                            url: '../drawCashrecord/drawCashrecord',
                        })
                    }
                }
            })
    },
    allCash: function() {
        var that = this;
        that.setData({
            cashNum: that.data.balance
        })
    }
})